﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace VoteUphold
{

    /// <summary>
    /// 发送Email类//MailMessage/SmtpClient
    /// </summary>
    public class Email
    {
        private static SmtpClient client;
        public void Send(string subject,string body)
        {
           //string aaa= EmailInfo.BinarySerialize(new UserInfo() { DisplayName = "小鸟", Password = "", UserName = "feiazifeiazi@163.com" });
            string base64String = "AAEAAAD/////AQAAAAAAAAAMAgAAAEFWb3RlVXBob2xkLCBWZXJzaW9uPTEuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbAMBAAAAE1ZvdGVVcGhvbGQuVXNlckluZm8DAAAAGTxVc2VyTmFtZT5rX19CYWNraW5nRmllbGQcPERpc3BsYXlOYW1lPmtfX0JhY2tpbmdGaWVsZBk8UGFzc3dvcmQ+a19fQmFja2luZ0ZpZWxkAgAAAAb/////FGZlaWF6aWZlaWF6aUAxNjMuY29tBv////8G5bCP6bifBv////8QcXNpb2ZnNG51aTg5NzU3NAs=";
            UserInfo userInfo = EmailInfo.GetUserInfo(base64String);
            MailAddress from = new MailAddress(userInfo.UserName, userInfo.DisplayName);
            MailAddress to = new MailAddress("605064709@qq.com", "qqqqqq6050");
            MailMessage message = new MailMessage(from, to);
            message.Subject = subject;
            message.Body = body;
            //MailAddress bcc = new MailAddress("404313364@qq.com");
            //message.Bcc.Add(bcc);
            if (client==null)
            {
                client = Get163Smtp(userInfo);
            }
            try
            {
                for (int i = 0; i < 5; i++)
                {
                    client.Send(message);
                }
                
            }
            catch (Exception ex)
            {
            }
            
        }


        /// <summary>
        /// 获取 server
        /// </summary>
        /// <returns></returns>
        private SmtpClient Get163Smtp(UserInfo userInfo)
        {
            SmtpClient client = new SmtpClient(EmailInfo.HOST_163,EmailInfo.PROT_163);// new SmtpClient(server);
            client.Credentials = CredentialCache.DefaultNetworkCredentials;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential(userInfo.UserName, userInfo.Password);
            return client;
        }
    }
}